Vue.filter('date',time=>moment(time).format('DD/MM/YY, HH:mm'));
new Vue({
	el: '#notebook',
	data() {
		return {
			//content: localStorage.getItem("content")||"You can write in **markdown**",
			notes: JSON.parse(localStorage.getItem('notes'))||[],
			selectedId: localStorage.getItem("selected-id")||null,
		}
	},
	computed: {
		notePreview: function() {
			return this.selectedNote ? marked(this.selectedNote.content) : '';
		},
		addButtonTitle: function() {
			return notes.length + "note(s) already.";
		},
		selectedNote: function() {
			return this.notes.find(note => note.id === this.selectedId);
		},
		sortedNotes(){
			return this.notes.slice().sort((a,b)=>a.created-b.created).sort((a,b)=>(a.favorite===b.favorite)?0:a.favorite?-1:1);
		},
		linesCount(){
			if(this.selectNode){
				return this.selectedNote.content.split(/\r\n|\r|\n/).length;
			}
		},
		wordsCount(){
			if(this.selectedNote){
				var s=this.selectedNote.content
				s=s.replace(/\n/g,'');
				s=s.replace(/(^\s*)|(\s*$)/gi,'');
				s=s.replace(/\s\s+/gi,'');
				return s.split(' ').length;
			}
		},
		charactersCount(){
			if(this.selectedNote){
				return this.selectedNote.content.split('').length;
			}
		}
	},
	methods: {
		// strong: function() {
		// 	var t = document.getElementById("textarea");
		// 	t.focus();
		// 	var start = t.selectionStart;
		// 	var end = t.selectionEnd;
		// 	var text = t.value;
		// 	text.substring(start, end);
		// 	text = text.substring(0, start) + "**" + text.substring(start, end) + "**" + text.substring(end, text.length);
		// 	this.content = text;
		// },
		/*saveNote: function(val) {
			localStorage.setItem("content", val);
			console.log("save note:", val);
		},*/
		addNote: function() {
			const time = Date.now();//用var就是变成全局变量了，就变成引用了好像
			const note = {
				id: String(time),
				title: 'new note' + (this.notes.length + 1),
				content: '**Hi** This notebook is using [markdown](https://github/adam-p/markdown-here/wiki/Markdown-Cheatsheet) for formatting!',
				created: time,
				favorite: false,
			}
			this.notes.push(note);
		},
		selectNote(note) {
			this.selectedId = note.id;
		},
		saveNotes: function() {
			localStorage.setItem('notes', JSON.stringify(this.notes));
			console.log('Notes saved', new Date());
		},
		removeNote(){
			if(this.selectedNote&&confirm('Delete the note?'))
			{
				const index=this.notes.indexOf(this.selectedNote);
				if(index!==-1)
				{
					this.notes.splice(index,1);
				}
			}
		},
		favoriteNote(){
			this.selectedNote.favorite=!this.selectedNote.favorite;
		}
	},
	watch: {
		//content: 'saveNote',
		notes: {
			handler: 'saveNotes',
			deep: true,
		},
		selectedId:function(val){
			localStorage.setItem("selected-id",val);
		},
	},
	// created() {
	// 	this.content = localStorage.getItem("content") || "You can write in **markdown**";
	// }
});

//const html = marked('**Bold** *Italic* [link](http:vuejs.org/)');
//console.log(html);

function insertStr(source, start, end, startStr, endStr) //对textarea中鼠标选中的字符串的两边进行加工
{
	return source.slice(0, start) + startStr + source.slice(start, end) + endStr + source.slice(end);
}
// function sortnote(a,b)
// {
// 	if(a.favorite===b.favorite)return a.favorite-b.favorite;
// }
/*
function strong() {
	var t = document.getElementById("textarea");
	t.focus();
	var start = t.selectionStart;
	var end = t.selectionEnd;
	var text = t.value;
	text.substring(start, end);
	text = text.substring(0, start) + "**" + text.substring(start, end) + "**" + text.substring(end, text.length);
	t.value = text;
};*/
